<?php

/**
 * Classe prenant en charge les requetes SQL de type [CREATE TABLE]
 *
 * @category   McCube
 * @package    Db
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 * @see        http://www.php.net/manual/book.pdo.php
 */
class McCube_Db_Query_CreateTable extends McCube_Db_Query{

    protected $_keys   = array();
    protected $_engine = 'INNODB';

    public function engine($engine){
        $this->_engine = $engine;
        return $this;
    }

    public function build(){
        return 'CREATE TABLE `'.$this->_table.'` (
            '.implode(' , ', $this->_fields).'
            '.($this->_keys ? ','.implode(' , ', $this->_keys) : '').'
        ) ENGINE = '.$this->_engine.';';
    }

    public function uniqueKey($name){
        return $this->addKey($name, 'UNIQUE');
    }

    public function indexKey($name){
        return $this->addKey($name, 'INDEX');
    }

    public function key($name, $type){
        $this->_keys[$name] = 'ADD '.$type.' (  `'.$name.'` )';
        return $this;
    }

    /**
     * ON* : CASCADE | SET NULL | NO ACTION | RESTRICT
     */
    public function foreignKey($name, $tableRef, $nameRef, $onDelete=null, $onUpdate=null){
        $this->_keys[$name] = 'FOREIGN KEY ( '.$name.' ) REFERENCES '.$tableRef.'( '.$nameRef.' )';

        if($onDelete)
            $this->_keys[$name] .= ' ON DELETE '.$onDelete;

        if($onUpdate)
            $this->_keys[$name] .= ' ON UPDATE '.$onUpdate;

        return $this;
    }

    public function stringField($name, $config){
        return $this->addField($name, $config);
    }

    public function integerField($name, $config){
        return $this->addField($name, $config);
    }

     public function floatField($name, $config){
        return $this->addField($name, $config);
    }

    public function timeField($name, $config){
        return $this->addField($name, $config);
    }

    public function field($name, $config){
        if(!empty($config['unique' ])) $this->uniqueKey ($name);
	    if(!empty($config['index'  ])) $this->indexKey  ($name);

        $this->_fields[$name] = '`'.$name.'` '.$config['type'];

        if(isset($config['length'])){
            $this->_fields[$name] .= '('.$config['length'];
            if(isset($config['decimal'])){
                $this->_fields[$name] .= ','.$config['decimal'];
            }
            $this->_fields[$name] .= ')';
        }

        $this->_fields[$name] .=  empty($config['null']) ? ' NOT NULL' : ' NULL';
        $this->_fields[$name] .= !empty($config['auto-increment']) ? ' AUTO_INCREMENT' : '';
        $this->_fields[$name] .= !empty($config['primary']) ? ' PRIMARY KEY' : '';

        return $this;
    }

    public function fields($fields){
        foreach($fields as $name => $config){
            $method = $config['type'].'Field';
            if(method_exists($this, $method)){
                $this->$method($name, $config);
            } else{
                $this->field($name, $config);
            }
        }
        return $this;
    }
}